package com.yndsoft.jc.ui.memberpage.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseActivity;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.utils.AppSession;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.supplier.widget.dialog.SupplierDialog;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;

import butterknife.Bind;

public class ChangePswActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.base_backBtn)
    RelativeLayout base_backBtn;
    @Bind(R.id.base_title)
    TextView base_title;

    @Bind(R.id.changepsw_new_psw)
    EditText changepsw_new_psw;
    @Bind(R.id.changepsw_new_psw_confirm)
    EditText changepsw_new_psw_confirm;
    @Bind(R.id.change_pwd_complete)
    Button change_pwd_complete;
    private String tag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_psw);
        setViews();
        setListener();
    }

    private void setViews() {

        tag = getIntent().getStringExtra(Constants.TAG);
        base_title.setText(getResources().getString(R.string.change_psw));
    }

    private void setListener() {
        base_backBtn.setOnClickListener(this);
        change_pwd_complete.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.base_backBtn:
                finish();
                break;
            case R.id.change_pwd_complete:
                String pwd1 = changepsw_new_psw.getText().toString();
                String pwd2 = changepsw_new_psw_confirm.getText().toString();
                if (TextUtils.isEmpty(pwd1)) {
                    showHint("请输入新密码！");
                } else {
                    if (TextUtils.isEmpty(pwd2)) {
                        showHint("请输入确认密码！");
                    } else {
                        if (!pwd1.equals(pwd2)) {
                            showHint("两次输入的密码不一致！");
                        } else {
                            if (null != tag && tag.equals(Constants.SUPPLIER)) {
                                changeSupplierPwd(pwd1);
                            } else {
                                changePwd(pwd1);
                            }
                        }
                    }
                }
                break;
        }
    }

    private void showHint(String content) {
        new SupplierDialog(mContext).show(false, "提示", content);
    }

    private void changePwd(String pwd) {
        mHttpUtil.resettingPsw(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        ToastUtil.show(mContext, "修改失败");
                        break;
                    case 1:
                        ToastUtil.show(mContext, "修改成功");
                        finish();
                        break;
                }
            }
        }, "changepwd", mSession.getUserInfo().getId(), pwd);
    }

    private void changeSupplierPwd(String pwd) {
        mHttpUtil.updatePwd(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        ToastUtil.show(mContext, "修改失败");
                        break;
                    case 1:
                        ToastUtil.show(mContext, "修改成功");
                        finish();
                        break;
                }
            }
        }, "changepwd", AppSession.getInstance(mContext).getId(), pwd);
    }
}
